#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>
#include "tree.h"

using namespace std;
bool find1 = false;
Tree *lcs(Tree *root, int l, int r)
{
    if (!root) return NULL;
    if (root->data == l) 
        return root;
    if (root->data == r)
        return root;
    Tree *left = lcs(root->left, l, r);
    Tree *right = lcs(root->right, l, r);
    if(left && right) {cout << left->data << " " << right->data << endl;find1 = true; return root;}
    return left ? left : right; 
}

int main(int argc, char **argv)
{
    int input[] = {21,7,5,6,3,2,1,4,6,23,43,54,12,54,8,10,11,35,56};
    //int input[] = {7};
    Tree *root = NULL, *root2 = NULL;
    int result;

    for (int i = 0; i < sizeof(input)/sizeof(int); i++) {
        TreeInsert(root, input[i]);
    }
    print_ascii_tree(root);
    Tree *node = lcs(root, 6, 11);
    if (!node) cout << "lcs does not excist!" << endl;
    else cout << "lcs is " << node->data << endl;
    find1 = false;
    node = lcs(root, -1, 11);
    if (!node || !find1) cout << "lcs does not excist!" << endl;
    else cout << "lcs is " << node->data << endl;
    node = lcs(root, 43, 7);
    if (!node) cout << "lcs does not excist!" << endl;
    else cout << "lcs is " << node->data << endl;
}
